#############################################
# 1 项目配置
#############################################

# set(PROJECT_NAME "ProjectName")

cmake_minimum_required( VERSION 3.0.0 )
project( ${PROJECT_NAME} VERSION 0.1.0 )

message(STATUS "=======================================")
message(STATUS "项目名称")
message(STATUS "---------------------------------------")
message(STATUS "PROJECT_NAME:${PROJECT_NAME}")
message(STATUS "PROJECT_VERSION:${PROJECT_VERSION}")
message(STATUS "PROJECT_VERSION_MAJOR:${PROJECT_VERSION_MAJOR}")
message(STATUS "PROJECT_VERSION_MINOR:${PROJECT_VERSION_MINOR}")
message(STATUS "PROJECT_VERSION_PATCH:${PROJECT_VERSION_PATCH}")
message(STATUS "---------------------------------------")

#############################################
# 1 设置编译版本
#############################################

# set(CMAKE_BUILD_TYPE "Release")
if(${CMAKE_BUILD_TYPE} MATCHES "Release")
    message(STATUS "Release版本")
    set(BuildType "Release")
else()
    set(BuildType "Debug")
endif()

message(STATUS "=======================================")
message(STATUS "编译样式")
message(STATUS "---------------------------------------")
message(STATUS "${BuildType}版本")
message(STATUS "---------------------------------------")

#############################################
# 1 设置bin(EXECUTABLE_OUTPUT_PATH)和lib(LIBRARY_OUTPUT_PATH)库目录
#############################################

# 设置安装根目录??
# SET(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR}/install)

# ??
# SET(DESTINATION ${PROJECT_SOURCE_DIR}/install)

set(RELEASE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/release)
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
    SET(LIBRARY_OUTPUT_PATH ${RELEASE_DIR}/linux/${BuildType})
    SET(EXECUTABLE_OUTPUT_PATH ${RELEASE_DIR}/linux/${BuildType})
    add_compile_options(-fPIC) # ??
elseif (CMAKE_SYSTEM_NAME MATCHES "Windows")
    SET(LIBRARY_OUTPUT_PATH ${RELEASE_DIR}/windows/${BuildType})
    SET(EXECUTABLE_OUTPUT_PATH ${RELEASE_DIR}/windows/${BuildType})
elseif (CMAKE_SYSTEM_NAME MATCHES "Darwin")
    SET(LIBRARY_OUTPUT_PATH ${RELEASE_DIR}/mac/${BuildType})
    SET(EXECUTABLE_OUTPUT_PATH ${RELEASE_DIR}/mac/${BuildType})
endif ()

# 添加自己的库搜索路径
LINK_DIRECTORIES(${LIBRARY_OUTPUT_PATH})

message(STATUS "=======================================")
message(STATUS "目录设置")
message(STATUS "---------------------------------------")
message(STATUS "LIBRARY_OUTPUT_PATH:${LIBRARY_OUTPUT_PATH}")
message(STATUS "EXECUTABLE_OUTPUT_PATH:${EXECUTABLE_OUTPUT_PATH}")
message(STATUS "---------------------------------------")

# ??
# # SET(DESTINATION ${PROJECT_SOURCE_DIR}/install)
# message(STATUS "=======================================")
# message(STATUS "路径设置")
# message(STATUS "---------------------------------------")
# message(STATUS "PROJECT_NAME:${PROJECT_NAME}")
# message(STATUS "PROJECT_BINARY_DIR:${PROJECT_BINARY_DIR}")
# message(STATUS "PROJECT_SOURCE_DIR:${PROJECT_SOURCE_DIR}")
# message(STATUS "CMAKE_INSTALL_PREFIX:${CMAKE_INSTALL_PREFIX}")
# # message(STATUS "DESTINATION:${DESTINATION}")
# message(STATUS "---------------------------------------")



#############################################
# 3 编译选项
#############################################

# 使能c++11
set(CMAKE_CXX_STANDARD 11)
# set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

if(${CMAKE_BUILD_TYPE} MATCHES "Release")
    set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat=0")
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wformat=0")
else()
    set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wformat=0")
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wformat=0")
endif()

# set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-stack-check")
# set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-stack-check")


message(STATUS "=======================================")
message(STATUS "编译选项")
message(STATUS "---------------------------------------")
message(STATUS "CMAKE_C_FLAGS:${CMAKE_C_FLAGS}")
message(STATUS "CMAKE_CXX_FLAGS:${CMAKE_CXX_FLAGS}")
message(STATUS "---------------------------------------")


#############################################
# 3 编译选项
#############################################

# 增加预定义
add_definitions(
    -DLYL_DEBUG
)

message(STATUS "=======================================")
message(STATUS "库搜索路径")
message(STATUS "---------------------------------------")
get_property(temp_directories DIRECTORY PROPERTY COMPILE_DEFINITIONS)
message(STATUS "COMPILE_DEFINITIONS: ${temp_directories}")
get_property(temp_directories DIRECTORY PROPERTY COMPILE_OPTIONS)
message(STATUS "COMPILE_OPTIONS: ${temp_directories}")
message(STATUS "---------------------------------------")

#############################################
# 4 头文件搜索路径
#############################################

INCLUDE_DIRECTORIES(
    /usr/include
)

message(STATUS "=======================================")
message(STATUS "头文件搜索路径")
message(STATUS "---------------------------------------")
get_property(temp_directories DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
message(STATUS "INCLUDE_DIRECTORIES: ${temp_directories}")
message(STATUS "---------------------------------------")

#############################################
# 5 库文件搜索路径
#############################################

link_directories(
    /usr/libs
)

message(STATUS "=======================================")
message(STATUS "库搜索路径")
message(STATUS "---------------------------------------")
get_property(temp_directories DIRECTORY PROPERTY LINK_DIRECTORIES)
message(STATUS "LINK_DIRECTORIES: ${temp_directories}")
message(STATUS "---------------------------------------")

#############################################
# 6 源码路径
#############################################

aux_source_directory(./src SRC_LIST)

message(STATUS "=======================================")
message(STATUS "项目源文件列表")
message(STATUS "---------------------------------------")
foreach(src_filename ${SRC_LIST})
    message(STATUS ${src_filename})
endforeach(src_filename)
message(STATUS "---------------------------------------")

#############################################
# 7 目标生成
#############################################

add_executable( ${PROJECT_NAME} ${SRC_LIST} )

message(STATUS "=======================================")
message(STATUS "编译选项")
message(STATUS "---------------------------------------")
get_property(temp_target TARGET ${PROJECT_NAME} PROPERTY COMPILE_FLAGS)
message(STATUS "COMPILE_FLAGS: ${temp_target}")
get_property(temp_target TARGET ${PROJECT_NAME} PROPERTY COMPILE_OPTIONS)
message(STATUS "COMPILE_OPTIONS: ${temp_target}")
get_property(temp_target TARGET ${PROJECT_NAME} PROPERTY COMPILE_DEFINITIONS)
message(STATUS "COMPILE_DEFINITIONS: ${temp_target}")
get_property(temp_target TARGET ${PROJECT_NAME} PROPERTY LINK_DEPENDS)
message(STATUS "LINK_DEPENDS: ${temp_target}")
get_property(temp_target TARGET ${PROJECT_NAME} PROPERTY LINK_FLAGS)
message(STATUS "LINK_FLAGS: ${temp_target}")
message(STATUS "---------------------------------------")

#############################################
# 8 连接动态库静态库
#############################################

set(
    LINK_LIB_LIST 
    -lpthread
)

target_link_libraries(
    ${PROJECT_NAME}
    ${LINK_LIB_LIST}
)

message(STATUS "=======================================")
message(STATUS "连接库列表")
message(STATUS "---------------------------------------")
get_property(temp_target TARGET ${PROJECT_NAME} PROPERTY LINK_LIBRARIES)
message(STATUS "LINK_LIBRARIES: ${temp_target}")
message(STATUS "---------------------------------------")

#############################################
# 9 安装配置
#############################################

# make install时的可执行文件输出路径（相对于CMAKE_INSTALL_PREFIX的目录）
# install(TARGETS ${PROJECT_NAME} DESTINATION bin)
install(TARGETS ${PROJECT_NAME} DESTINATION .) 

#############################################
# 结束
#############################################
